#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 KT
#
# Author: Choonho Son <choonho.son@kt.com>
#

__version__="1.0"

import os
import logging
import logging.handlers
import sys

# Log Settings
DEFAULT_LOG_LEVEL = logging.DEBUG
MAX_BYTES = 10*1024*1024
BACKUP_COUNT = 10

LOG_PERMIT = 0640
LOG_PATH = '/var/log'
LOG_FILE = LOG_PATH + '/cloudcfg.log'

def setLogger(logger_name, log_file):
	logger = logging.getLogger(logger_name)
	logger.setLevel(DEFAULT_LOG_LEVEL)

	# create file rotate handler
	rh = logging.handlers.RotatingFileHandler(log_file, maxBytes=MAX_BYTES, backupCount=BACKUP_COUNT)
	rh.setLevel(DEFAULT_LOG_LEVEL)

	# create console handler with a higher log level
	ch = logging.StreamHandler()
	ch.setLevel(DEFAULT_LOG_LEVEL)

	# create formatter and add it to the handlers
	DATE_FORMAT = "%b %d %H:%M:%S"
	LOG_FORMAT = "%(asctime)s [%(levelname)s] (%(filename)s:%(lineno)d) %(message)s"

	FILE_FORMAT = logging.Formatter(LOG_FORMAT, DATE_FORMAT)
	CONSOLE_FORMAT = logging.Formatter("[%(levelname)s] (%(filename)s:%(lineno)d) %(message)s")

	# set each handler format
	ch.setFormatter(CONSOLE_FORMAT)
	rh.setFormatter(FILE_FORMAT)

	# add the handlers to logger
	logger.addHandler(rh)
	logger.addHandler(ch)


# Initialize Logger
setLogger('cloudcfg-log', LOG_FILE)
	
class Log:
	def __init__(self, logger_name):
		self.LOGGER_NAME = logger_name

	def getLogger(self, executor='installer'):
		logger = logging.getLogger(self.LOGGER_NAME)
		return logger

class CloudLog(Log):
	def __init__(self):
		Log.__init__(self, 'cloudcfg-log')

if __name__ == '__main__':
	test = CloudLog().getLogger()
	test.debug("TEST")
	
